package com.qf.web.servlet;

import com.alibaba.fastjson.JSON;
import com.qf.domain.WeiXin;
import com.qf.service.OrderService;
import com.qf.service.impl.OrderServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//设置支付的
@WebServlet(name = "ParServlet",value = "/paySuccess")
public class ParServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String result = request.getParameter("result");
        WeiXin weiXin = JSON.parseObject(result, WeiXin.class);
        if (weiXin.getType()==0){//重定向
            if (weiXin.getResult().getResult_code().equals("SUCCESS")){
                //修改订单状态
                OrderService orderService=new OrderServiceimpl();
                try {
                    orderService.UpdateOrderstatus(weiXin.getResult().getOut_trade_no(),"2");
                    //支付成功
                    request.setAttribute("msg","支付成功");
                    request.getRequestDispatcher("/message.jsp").forward(request,response);
                }catch (Exception e){
                    e.printStackTrace();
                    request.setAttribute("msg","支付失败");
                    request.getRequestDispatcher("/message.jsp").forward(request,response);
                }
            }
        }else if (weiXin.getType()==1){
            //点对点通信
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
